package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ServiceClassTransferMapper;
import com.ruoyi.system.domain.ServiceClassTransfer;
import com.ruoyi.system.service.IServiceClassTransferService;

/**
 * 业务类划转-CRUDService业务层处理
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
public class ServiceClassTransferServiceImpl implements IServiceClassTransferService
{
    @Autowired
    private ServiceClassTransferMapper serviceClassTransferMapper;

    /**
     * 查询业务类划转-CRUD
     *
     * @param tranId 业务类划转-CRUD主键
     * @return 业务类划转-CRUD
     */
    @Override
    @DataScope(deptAlias = "sct",userAlias = "sct")
    public ServiceClassTransfer selectServiceClassTransferByTranId(Long tranId)
    {
        return serviceClassTransferMapper.selectServiceClassTransferByTranId(tranId);
    }

    /**
     * 查询业务类划转-CRUD列表
     *
     * @param serviceClassTransfer 业务类划转-CRUD
     * @return 业务类划转-CRUD
     */
    @Override
    @DataScope(deptAlias = "sct",userAlias = "sct")
    public List<ServiceClassTransfer> selectServiceClassTransferList(ServiceClassTransfer serviceClassTransfer)
    {
        return serviceClassTransferMapper.selectServiceClassTransferList(serviceClassTransfer);
    }

    /**
     * 新增业务类划转-CRUD
     *
     * @param serviceClassTransfer 业务类划转-CRUD
     * @return 结果
     */
    @Override
    public int insertServiceClassTransfer(ServiceClassTransfer serviceClassTransfer)
    {
        return serviceClassTransferMapper.insertServiceClassTransfer(serviceClassTransfer);
    }

    /**
     * 修改业务类划转-CRUD
     *
     * @param serviceClassTransfer 业务类划转-CRUD
     * @return 结果
     */
    @Override
    public int updateServiceClassTransfer(ServiceClassTransfer serviceClassTransfer)
    {
        return serviceClassTransferMapper.updateServiceClassTransfer(serviceClassTransfer);
    }

    /**
     * 批量删除业务类划转-CRUD
     *
     * @param tranIds 需要删除的业务类划转-CRUD主键
     * @return 结果
     */
    @Override
    public int deleteServiceClassTransferByTranIds(Long[] tranIds)
    {
        return serviceClassTransferMapper.deleteServiceClassTransferByTranIds(tranIds);
    }

    /**
     * 删除业务类划转-CRUD信息
     *
     * @param tranId 业务类划转-CRUD主键
     * @return 结果
     */
    @Override
    public int deleteServiceClassTransferByTranId(Long tranId)
    {
        return serviceClassTransferMapper.deleteServiceClassTransferByTranId(tranId);
    }
    @Override
    public String importTransfer(List<ServiceClassTransfer> dataManagementList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(dataManagementList) || dataManagementList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<ServiceClassTransfer> existList = selectServiceClassTransferList(null);
        for (ServiceClassTransfer importData : dataManagementList)
        {
            try {

                boolean userFlag = false;
                for (ServiceClassTransfer entry : existList) {
                    if (entry.getCollectionItemNo().equals(importData.getCollectionItemNo())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertServiceClassTransfer(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getCollectionItemNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateServiceClassTransfer(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getCollectionItemNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getCollectionItemNo() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + importData.getCollectionItemNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
